package com.module.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.module.admin.app.service.AppInfoService;
import com.module.admin.sys.pojo.SysUser;
import com.ms.client.task.api.service.ApiTaskJobService;
import com.ms.monitor.init.DbUpdate;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;

/**
 * 系统配置的Controller
 * @author  yuejing
 * @email   yuejing0129@163.com 
 * @date    2015年4月5日 下午10:21:22 
 * @version 1.0.0
 */
@Controller
public class ViewController extends BaseController {

	@Autowired
	private AppInfoService appInfoService;
	@Autowired
	private ApiTaskJobService apiTaskJobService;
	
	@RequestMapping(value = "/")
	public String index(HttpServletRequest request) {
		return "redirect:index.jsp";
	}
	
	@RequestMapping(name = "执行升级脚本", value = "/dbUpdate")
	@ResponseBody
	public ResponseFrame dbUpdate(HttpServletRequest request) {
		ResponseFrame frame = new ResponseFrame();
		DbUpdate.init();
		frame.setSucc();
		return frame;
	}

	@RequestMapping(value = "/view/f-json/cardInfo")
	@ResponseBody
	public void cardInfo(HttpServletRequest request, HttpServletResponse response,
			String type) {
		ResponseFrame frame = new ResponseFrame();
		try {
			SysUser user = getSessionUser(request);
			Map<String, Object> data = new HashMap<String, Object>();
			if ("appSum".equals(type)) {
				Map<String, Object> appSum = appInfoService.getAppSum(user);
				data.put("appSum", appSum);
			} else if ("ttsProject".equals(type)) {
				List<Map<String, Object>> projects = apiTaskJobService.findProjectidCount().getBody();
				/*Map<Integer, String> projectnames = new HashMap<Integer, String>();
				for (Map<String, Object> map : projects) {
					Integer projectid = FrameMapUtil.getInteger(map, "projectid");
					String projectname = FrameMapUtil.getString(projectnames, projectid.toString());
					if(FrameStringUtil.isEmpty(projectname)) {
						TaskProject taskProject = apiTaskProjectService.get(projectid).getBody();
						projectname = taskProject.getName();
						projectnames.put(projectid, projectname);
					}
					map.put("projectname", projectname);
				}*/
				data.put("projects", projects);
			}
			frame.setBody(data);
			frame.setSucc();
		} catch (Exception e) {
			LOGGER.error("分页获取信息异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}
}